// import { routerRedux } from "dva/router";
import {
  editSave,
  getFormDetail,
  executeRequest,
} from "../server/formBuilderApi";

const initData = {
  formDetailData: {},
};

export default {
  namespace: "FormBuilder",
  state: {
    ...initData,
  },

  effects: {
    *getFormDetail({ payload }, { call, put }) {
      const resData = yield call(getFormDetail, payload);
      const { errCode, errMsg, datas = {}, errortext } = resData || {};
      yield put({ type: "get/formDetailData", payload: datas || {} });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *editSave({ payload }, { call }) {
      const { errCode, errMsg, datas, errortext } = yield call(
        editSave,
        payload,
      );
      if (errCode === 0) {
        // flowmanage/formbuilder页面引用了@ali-dayu/form-editor v0.0.8，对antdv4强依赖导致覆盖了项目中antdv3的样式，
        // 需重新加载html去除antdv4的相关css
        // location.href = '/flow-web/flowmanage/formconfigure';
        location.href = `${window.routerBase || '/'}flowmanage/formconfigure`;
        /*
        yield put(
          routerRedux.push({
            pathname: "/flowmanage/formconfigure",
          }),
        );
        */
      }
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *executeRequest({ payload }, { call }) {
      const resData = yield call(executeRequest, payload);
      const { errCode, errMsg, datas = {}, errortext } = resData || {};

      return { errCode, errMsg: errortext || errMsg, datas };
    },
  },
  reducers: {
    "get/formDetailData": (state, { payload }) => ({
      ...state,
      formDetailData: payload,
    }),
    clearData: () => ({ ...initData }),
  },
};
